//
//  FileTypeHelper.swift
//  ZipTool
//
//  Created by jerry on 2025/8/15.
//

import Foundation
import UniformTypeIdentifiers

struct FileTypeHelper {
    
    static let supportedArchiveTypes: [UTType] = [
        UTType(filenameExtension: "zip")!,
        UTType(filenameExtension: "7z")!,
        UTType(filenameExtension: "tar")!,
        UTType(filenameExtension: "gz")!,
        UTType(filenameExtension: "rar")!
    ]
    
    static let supportedCompressionTypes: [UTType] = [
        UTType(filenameExtension: "zip")!,
        UTType(filenameExtension: "tar")!,
        UTType(filenameExtension: "gz")!
    ]
    
    static func isArchiveFile(_ url: URL) -> Bool {
        let pathExtension = url.pathExtension.lowercased()
        let fileName = url.lastPathComponent.lowercased()
        
        switch pathExtension {
        case "zip", "7z", "tar", "rar":
            return true
        case "gz":
            return fileName.hasSuffix(".tar.gz")
        default:
            return false
        }
    }
    
    static func isCompressionSupported(_ url: URL) -> Bool {
        let pathExtension = url.pathExtension.lowercased()
        let fileName = url.lastPathComponent.lowercased()
        
        switch pathExtension {
        case "zip", "tar":
            return true
        case "gz":
            return fileName.hasSuffix(".tar.gz")
        default:
            return false
        }
    }
    
    static func getArchiveTypeDescription(_ url: URL) -> String {
        let pathExtension = url.pathExtension.lowercased()
        let fileName = url.lastPathComponent.lowercased()
        
        switch pathExtension {
        case "zip":
            return "ZIP 压缩包"
        case "7z":
            return "7-Zip 压缩包"
        case "tar":
            return "TAR 归档文件"
        case "gz":
            if fileName.hasSuffix(".tar.gz") {
                return "TAR.GZ 压缩包"
            }
            return "GZIP 压缩文件"
        case "rar":
            return "RAR 压缩包"
        default:
            return "未知格式"
        }
    }
    
    static func getFileIcon(for entry: SimpleArchiveEntry) -> String {
        if entry.isDirectory {
            return "folder.fill"
        }
        
        let pathExtension = URL(fileURLWithPath: entry.name).pathExtension.lowercased()
        
        switch pathExtension {
        case "txt", "md", "rtf":
            return "doc.text.fill"
        case "pdf":
            return "doc.richtext.fill"
        case "jpg", "jpeg", "png", "gif", "bmp", "tiff":
            return "photo.fill"
        case "mp4", "mov", "avi", "mkv":
            return "video.fill"
        case "mp3", "wav", "aac", "flac":
            return "music.note"
        case "zip", "7z", "tar", "gz", "rar":
            return "archivebox.fill"
        case "app":
            return "app.fill"
        case "dmg":
            return "externaldrive.fill"
        default:
            return "doc.fill"
        }
    }
    
    static func formatFileSize(_ bytes: Int64) -> String {
        let formatter = ByteCountFormatter()
        formatter.allowedUnits = [.useBytes, .useKB, .useMB, .useGB]
        formatter.countStyle = .file
        return formatter.string(fromByteCount: bytes)
    }
}